package com.zzu.view.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.zzu.R;
import com.zzu.model.bean.User;
import com.zzu.presenter.presenter.UserPresenter;
import com.zzu.presenter.impl.UserPresenterImpl;
import com.zzu.view.view.UserResponseView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName RegisterActivity
 * @Description TODO
 * @Author ZB
 * @Date 2021/5/5 18:58
 * @Version 1.0
 **/
public class RegisterActivity extends Activity{

    private final String TAG = RegisterActivity.class.getName();

    @BindView(R.id.et_register_name)
    EditText et_register_name;

    @BindView(R.id.et_register_tel)
    EditText et_register_tel;

    @BindView(R.id.et_register_password)
    EditText et_register_password;

    @BindView(R.id.et_register_re_password)
    EditText et_register_re_password;

    @BindView(R.id.bt_register_login)
    Button bt_register_login;

    @BindView(R.id.bt_register_register)
    Button bt_register_register;

    ProgressDialog dialog = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        // 初始化控件
        //ButterKnife和当前activity绑定
        ButterKnife.bind(this);

        // 初始化监听
        initListener();

    }

    private void initListener() {

        //为返回登录按钮添加监听效果
        bt_register_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //为注册按钮添加监听事件
        bt_register_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示ProgressDialog
                dialog = ProgressDialog.show(RegisterActivity.this, "提示", "正在加载中...");
                register();
            }
        });
    }


    private void register() {

        String name = et_register_name.getText().toString();
        String tel = et_register_tel.getText().toString();
        String password = et_register_password.getText().toString();
        String rePassword = et_register_re_password.getText().toString();

        if (TextUtils.isEmpty(name)) {
            //用户名为空
            Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return ;
        } else if (TextUtils.isEmpty(tel)) {
            Toast.makeText(RegisterActivity.this, "请输入电话号码", Toast.LENGTH_SHORT).show();
            return ;
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(RegisterActivity.this, "请确认密码",Toast.LENGTH_SHORT).show();
            return ;
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(RegisterActivity.this, "请输入确认密码", Toast.LENGTH_SHORT).show();
            return ;
        } else if (!password.equals(rePassword)) {
            Toast.makeText(RegisterActivity.this, "两次密码不一样，请重新输入", Toast.LENGTH_SHORT).show();
            return ;
        }

        UserPresenter userPresenter = new UserPresenterImpl(RegisterActivity.this,new UserResponseView<Boolean>() {
            @Override
            public void onResponse(Boolean response) {
                //判断电话号码是否存在
                if(response){
                    //手机号已存在
                    Toast.makeText(RegisterActivity.this,"手机号已存在",Toast.LENGTH_SHORT).show();
                }else{
//                    Toast.makeText(RegisterActivity.this,"手机号可以使用",Toast.LENGTH_SHORT).show();
                    User user = new User(tel, name, password);
                    UserPresenter register = new UserPresenterImpl(RegisterActivity.this,new UserResponseView() {
                        @Override
                        public void onResponse(Object response) {
                            //提示注册成功
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            //注册成功，跳转到登录页面
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent);
                            //移除ProgressDialog
                            dialog.dismiss();
                            finish();
                        }
                    });
                    register.register(user);
                }
            }
        });
        //判断手机号是否存在
        userPresenter.checkIfUserExistByTel(tel);
    }

}






